<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Pisyek Kumar
 * @email pisyek@gmail.com
 * @link http://www.pisyek.com
 */
class Blog extends CI_Controller {

    public $ruta_template;

    function __construct() {
        parent::__construct();
        $this->lang->load('blg_'.$this->config->item('language'), $this->config->item('language'));
        $this->load->model('blog_model');
    }

    public function index() {
        $pages = 5;

        $data['posts'] = $this->blog_model->get_posts($pages, $this->uri->segment(3));
        $data['current'] = 'HOME';
        $data['categories'] = $this->blog_model->get_categories();

        $this->load->library('pagination'); //Cargamos la librería de paginación
        $config['uri_segment'] = 3;
        $config['base_url'] = site_url('front/blog'); // parametro base de la aplicación, si tenemos un .htaccess nos evitamos el index.php
        $config['per_page'] = $pages; 
        
        $config['num_links'] = 3; //Numero de links mostrados en la paginación
        $config['total_rows'] = $this->blog_model->get_num_posts();
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['next_tag_open'] = "<li class='next'>";
        $config['next_tag_close'] = "</li>";
        $config['prev_tag_open'] = "<li class='prev'>";
        $config['prev_tag_close'] = "</li>";
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config); 
        // render view
        $data['ruta'] = site_url() . RUTA_TEMPLATE;
        $data['output'] = $this->load->view('blog/index', $data, true);
        $this->load->view('blog/contenedor', $data);
    }

    public function about() {
        $data['current'] = 'ABOUT';
        $data['categories'] = $this->blog_model->get_categories();
        $this->template->write_view('content', 'blog/about', $data);
        $this->template->render();
    }

    public function post($id) { // get a post based on id
        $pages = 5; //Numero de registros mostrados por páginas
        $result = $this->blog_model->get_post($id);
        $data['query'] = $result;
        $data['comments'] = $this->blog_model->get_post_comment($id, $pages, $this->uri->segment(5)); // get comments related to the post
        $data['post_id'] = $id;
        $data['categories'] = $this->blog_model->get_categories();
        $data['ruta_template'] = $this->ruta_template;
        $this->load->library('pagination'); //Cargamos la librería de paginación
        if($result){
            $config['uri_segment'] = 5;
            $config['base_url'] = site_url('front/blog/post/'.$id); // parametro base de la aplicación, si tenemos un .htaccess nos evitamos el index.php
            $config['per_page'] = $pages; 
            $config['num_links'] = 3; //Numero de links mostrados en la paginación
            $config['total_rows'] = $result[0]->comment_count;
            $config['full_tag_open'] = "<ul class='pagination'>";
            $config['full_tag_close'] = "</ul>";
            $config['next_tag_open'] = "<li class='next'>";
            $config['next_tag_close'] = "</li>";
            $config['prev_tag_open'] = "<li class='prev'>";
            $config['prev_tag_close'] = "</li>";
            $config['cur_tag_open'] = '<li class="active"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $this->pagination->initialize($config); 
        }

        $this->load->helper('form');
        $data['ruta'] = site_url() . RUTA_TEMPLATE;
        $data['output'] = $this->load->view('blog/post', $data, true);
        $this->load->view('blog/contenedor', $data);
    }

    public function category($slug = FALSE) {
        $data['title'] = 'Category - ' . $this->config->item('site_title', 'ion_auth');
        $data['categories'] = $this->blog_model->get_categories();

        if ($slug == FALSE)
            show_404();
        else {
            $data['category'] = $this->blog_model->get_category(NULL, $slug); // get category details
            $data['query'] = $this->blog_model->get_category_post($slug); // get post in the category
            $data['years'] = $this->blog_model->get_years_post(); // get post in the category
        }
        $this->template->write_view('content', 'blog/category', $data);
        $this->template->render();
    }
    
    public function year($year = false, $month = false) {
        $data['title'] = 'Category - ' . $this->config->item('site_title', 'ion_auth');
        $data['categories'] = $this->blog_model->get_categories();

        if ($year == false && $month == false)
            show_404();
        else {
            $data['query'] = $this->blog_model->get_category_post(null, $year, $month); // get post in the category
            $data['years'] = $this->blog_model->get_years_post(); // get post in the category
        }
        $this->template->write_view('content', 'blog/category', $data);
        $this->template->render();
    }
    
    function add_comment(){
        if($this->ion_auth->user()->row()->id == '') { // block un-authorized access
            show_404();
        } else {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('p_name', lang('nombre'), 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('p_email', lang('email'), 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('p_website', lang('pagina_web'), 'required|max_length[200]|xss_clean');
            $this->form_validation->set_rules('p_comment', ucfirst(lang('comentario')), 'required|xss_clean');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('message', 'Faltan campos por rellenar');
                redirect('front/blog/post/'.$this->input->post('p_post'));
            } else {
                $this->blog_model->add_new_comment($this->input->post('p_post'),
                                                   $this->input->post('p_name'),
                                                   $this->input->post('p_email'),
                                                   $this->input->post('p_comment'),
                                                   $this->input->post('p_website'));
                $this->session->set_flashdata('message', '1 new comment added!');
                redirect('front/blog/post/'.$this->input->post('p_post'));
            }
        }
    }
}

/* End of file blog.php */
/* Location: ./application/controllers/blog.php */